home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 494 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  101 lines

  1. Path: news.sprintlink.net!datalytics!news
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help with Boolean class and templates
  5. Date: 4 Jan 1996 19:32:40 GMT
  6. Organization: Datalytics, Inc
  7. Message-ID: <4cha0o$ldg@gold.datalytics.com>
  8. References: <4bpb9t$dsh@onlink3.onlink.net>    <30E150AB.1ACF@cmt.lpr.mail.carel.fi> <ZLNMKWP.95Dec27142935@bridge.bst.bls.com>
  9. NNTP-Posting-Host: pc071.datalytics.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  14.  
  15. zlnmkwp@bridge.bst.bls.com (Alun Champion) wrote:
  16. >In article <30E150AB.1ACF@cmt.lpr.mail.carel.fi> Ari Lukumies <aril@cmt.lpr.mail.carel.fi> writes:
  17. >:  wfss16@onlink.net wrote:
  18. >
  19. >:  [snip]
  20. >:> Boolean Flag(1);
  21. >:> 
  22. >:> if(Flag)
  23. >:> yada yada yada...
  24. >:> 
  25. >:> What I mean is, can you get an object to do a default 
  26. >:> function so I don't have to do this:
  27. >:> 
  28. >:> if(Flag.GetValue())
  29. >:  [snip]
  30. >
  31. >:  You could, in your Boolean class, implement the operator int() 
  32. >[snip]
  33. >
  34. >if (..) will also accept void* conversions too. I have noticed that
  35. >many implementations use void* as the standard means of converting for if 
  36. >statements and return 'this' if the object is valid.
  37. >
  38.  
  39. FYI, here is how you would declare those functions.  I use 
  40. these same functions to allow ready detection of error states 
  41. in non-boolean classes.  (BTW, these functions are derived 
  42. from those in ios.)
  43.  
  44. class Boolean
  45. {
  46.     public:
  47.         DI_BOOLEAN    operator !(void) const;
  48.                 operator void*() const;
  49.     ...
  50. };
  51.  
  52. // Function:  operator !()
  53. // Description:  Indicates if there is an error in this
  54. // object.  It can be used like this:
  55. //    Boolean b(...);
  56. //    if (!b)            // if there is an error
  57. //    {
  58. //       ...
  59. //    }
  60. //
  61. // Modification History (date, name, description):
  62.  
  63. inline
  64. int Boolean::operator !(void) const
  65. {
  66.     return m_Flag != 1;
  67. }
  68.  
  69. // Function:  operator void*()
  70. // Description:  Indicates if there is no error in this
  71. // object.  It can be used like this:
  72. //    Boolean b(...);
  73. //    if (b)            // if there is no error
  74. //    {
  75. //       ...
  76. //    }
  77. //
  78. // NOTE:  The pointer is never valid; it serves only to
  79. // indicate the lack of errors.
  80. //
  81. // Modification History (date, name, description):
  82.  
  83. inline
  84. Boolean::operator void*(void) const
  85. {
  86.     return (void*)m_Flag;
  87. }
  88.  
  89. I didn't bother changing all the comments.  They are from my 
  90. DIErrorState class from which I inherit many others to provide 
  91. error state checking.  Thus, references to errors aren't 
  92. germane to a boolean class.
  93.  
  94. -- 
  95. Robert Stewart        | My opinions are usually my own.
  96. Datalytics, Inc.
  97. (513)226-7700
  98. stew@datalytics.com
  99.  
  100.  
  101.